﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR.Hosting;
using Microsoft.AspNet.SignalR.WebSockets;
using Cherrs.IM.Server.Core.Interface;

namespace Cherrs.IM.Server.Core
{
    public class User:Hub,IUser
    {
        
        
        public void register(string username)
        {
            
            if ((ShareData.userlist.Where(p => p.UserName == username)).Count() > 0)
            {
                
                Clients.Client(Context.ConnectionId).Registermessage("注册失败，用户名已经被使用");
            }
            else
            {
                ShareData.userlist.Add(new Model.ModelUser() { UserName = username });
                Clients.Client(Context.ConnectionId).Registermessage("注册成功！");
            }
            
            Console.WriteLine("请求的ID：" + Context.ConnectionId);
        }
        
        public void userlist()
        {
            
            StringBuilder sb = new StringBuilder();
            
            foreach (var i in ShareData.userlist)
            {
                sb.Append(i.UserName + "，");
            }
            
            Clients.Client(Context.ConnectionId).userlist(ShareData.userlist);
        }
        public override Task OnConnected()
        {
            
            Console.WriteLine(Context.ConnectionId);
            ShareData.co++;
            Console.WriteLine(ShareData.co);
            return base.OnConnected();
        }
        
    }
  




}
